<?php
class Business_Author
{
	protected $_tableInfo;
	protected $_table;
	
	public function __construct()
	{
		$this->_tableInfo = Zend_Registry::get('database')->author;
		$this->_table = Zend_Registry::get('database')->author->tablename;
	}
	
    public function getAuthorName($id)
    {
        $table = Zend_Registry::get('database')->author;
        return Utility_Db::getInstance()
            ->conn()
            ->select()
            ->from($table->tablename)
            ->where($table->id . '=?', $id)
            ->query()
            ->fetch();
    }
    
    public function getAllAuthors()
    {
        $table = Zend_Registry::get('database')->author;
        return Utility_Db::getInstance()
            ->conn()
            ->select()
            ->from($table->tablename)
            ->query()
            ->fetchAll();
    }
    
    /**
     * 保存作者
     * @param Array $data
     * @return Array
     */
    public function save($data)
    {
    	if (empty($data[$this->_tableInfo->id])) {
    		unset($data[$this->_tableInfo->id]);
    	}
    	$db = Utility_Db::getInstance()->conn();
    	$insRowNum = 0;
    	$affectRows = 0;
    	try {
    		$insRowNum = $db->insert($this->_table, $data);
    	} catch (Zend_Db_Exception $exp) {
    		if ($exp->getCode() == 1062) {
    			$authorid = $data[$this->_tableInfo->id];
    			$where = array(
    				$this->_tableInfo->id . ' = ?' => $authorid
    			);
    			unset($data[$this->_tableInfo->id]);
    			$affectRows = $db->update($this->_table, $data, $where);
    		}
    	}
    	
    	if ($affectRows || $insRowNum) {
    		$rtn = array(
    			'errorcode' => 0,
    			'errormsg' => 'success'
    		);
    	} else {
    		$rtn = array(
    			'errorcode' => -1,
    			'errormsg' => 'failed'
    		);
    	}
    	return $rtn;
    }
}